/*{
author=pk2.2（风铃等）
version=2.0
date=2023/03/19
}*/

/***
可以消耗资金和行动力来安置流民,增加城市人口.
***/

// ## 2023/05/03 # 铃 # 修复bug,美化显示。 ##
// ## 2023/05/02 # 铃 # 适配新的内政系统。 ##
// ## 2023/05/01 # 铃 # 完善了新的参数。 ##
// ## 2022/12/03 # 铃 # 新做了AI的安置流民功能。 ##

namespace 安置流民
{

	// ================ CUSTOMIZE ================
	// 全局常量 --------------------------------------------------
	/*[text=消耗技巧点 default=100 min=0 max=300]*/
	const int TP_COST = 0;

	/*[text=消耗行动力 default=50 min=20 max=50]*/
	const int ACTION_COST = 50;

	/*[text=消耗资金 default=3000 min=0 max=5000]*/
	const int gold_num = 1000;

	// ===========================================

	const int KEY = pk::hash("安置流民");

	class Main
	{

		pk::building @building_;
		pk::force @force;
		pk::person @taishu;
		pk::person @farmer;
		int building_id = -1;
		int province_id = 0;
		float deserter;

		Main()
		{
			pk::menu_item item;
			item.menu = 100;
			item.pos = 8;
			item.shortcut = "k";
			item.init = pk::building_menu_item_init_t(init);
			item.is_visible = pk::menu_item_is_visible_t(isVisible);
			item.is_enabled = pk::menu_item_is_enabled_t(isEnabled);
			item.get_text = pk::menu_item_get_text_t(getText);
			item.get_desc = pk::menu_item_get_desc_t(getDesc);
			item.handler = pk::menu_item_handler_t(handler);
			pk::add_menu_item(item);
		}

		void init(pk::building @building)
		{
			@building_ = @building;
			building_id = building.get_id();
			@force = pk::get_force(building.get_force_id());
			@taishu = pk::get_person(pk::get_taishu_id(building));
			@farmer = pk::get_person(武将_农民);
			province_id = pk::get_province_id(building.pos);

			deserter = ch::get_set_p(0).province_deserter[province_id];
		}

		bool isVisible()
		{
			// pk::trace("taishu.service" + taishu.service + "building_.get_id()" + building_.get_id());
			if (building_id == -1)
				return false;
			if (pk::get_taishu_id(building_) == -1)
				return false;
			return true;
		}

		bool isEnabled()
		{
			if (pk::is_absent(taishu) or pk::is_unitize(taishu))
				return false;
			else if (taishu.action_done == true)
				return false;
			else if (pk::get_gold(building_) < gold_num)
				return false;
			else if (force.tp < TP_COST)
				return false;
			else if (pk::get_district(pk::get_district_id(force, 1)).ap < ACTION_COST)
				return false;
			else if (int(deserter) < 1000)
				return false;
			return true;
		}

		string getText()
		{
			return pk::encode("安置流民");
		}

		string getDesc()
		{
			if (pk::is_absent(taishu) or pk::is_unitize(taishu))
				return pk::encode("太守不在.");
			else if (taishu.action_done == true)
				return pk::encode("太守已行动.");
			else if (pk::get_gold(building_) < gold_num)
				return pk::encode(pk::format("金钱不足.(至少需要{}金)", gold_num));
			else if (force.tp < TP_COST)
				return pk::encode(pk::format("技巧不足.(需要{}技巧)", TP_COST));
			else if (pk::get_district(pk::get_district_id(force, 1)).ap < ACTION_COST)
				return pk::encode(pk::format("行动力不足(需要{}行动力)", ACTION_COST));
			else if (int(deserter) < 1000)
				return pk::encode(pk::format("流民低于500人(现有{}人)", int(deserter)));
			else
				return pk::encode(pk::format("实施安置流民,现有{}人.(需要{}技巧, {}行动力)", int(deserter), TP_COST, ACTION_COST));
		}

		bool handler()
		{

			if (pk::choose(pk::encode("要实施安置流民吗?"), {pk::encode(" 是 "), pk::encode(" 否 ")}) == 1)
				return false;
			pk::scene(pk::scene_t(scene_Event));
			auto district = pk::get_district(pk::get_district_id(force, 1));
			pk::add_ap(district, -ACTION_COST);

			taishu.action_done = true;

			return true;
		}

		void scene_Event()
		{
			int forceid;
			int pop_gain_final;

			int n = pk::rand(10);

			if (n <= 10)
			{
				pk::fade(0);
				pk::sleep();
				pk::background(16);
				pk::fade(255);

				pk::message_box(pk::encode("百姓们，我已经找到了一片新的土地，那里有肥沃的田地。"), taishu);
				pk::message_box(pk::encode("我邀请你们跟随我，去那里安居乐业，重建我们的家园。你们愿意跟我一起走吗？"), taishu);
				pk::message_box(pk::encode("谢谢大人，我们愿跟随您!"), farmer);

				pk::building @base = pk::get_building(building_id);

				pk::message_box(pk::encode(pk::format("本州郡附近有流民\x1b[1x{}\x1b[0x人.", deserter)));

				pk::int_bool deal0;
				int max = pk::max(3000, pk::min(10000, pk::get_gold(base)));
				max = pk::min(int(deserter / 1.5), max);
				deal0 = pk::numberpad(pk::encode("花费金"), 0, max, 0, pk::numberpad_t(numberpad_t));
				int 花费量 = deal0.first;

				pop_gain_final = int(花费量 * 1.5);

				pk::message_box(pk::encode(pk::format("城市花费\x1b[1x{}\x1b[0x金来安置流民.", 花费量)));
				pk::message_box(pk::encode(pk::format("安置了\x1b[1x{}\x1b[0x人流民.", pop_gain_final)));

				pk::add_gold(building_, -花费量, true);
				pk::add_tp(force, -TP_COST, force.get_pos());
				ch::get_set_p(0).province_deserter[province_id] -= pop_gain_final;
				ch::add_population(building_id, pop_gain_final);

				pk::fade(0);
				pk::sleep();
				pk::background(-1);
				pk::fade(255);
			}
		}

		string numberpad_t(int line, int original_value, int current_value)
		{
			return pk::encode("");
		}
		// scene_Event()

	}

	Main main;
}